﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Service;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class CollegeHelper
    {
        CKCDB mydb = new CKCDB();
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<College> FindAll()
        {
            var apps = from n in mydb.Colleges
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            return apps;
        }
        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public College FindById(int id)
        {
            var app = mydb.Colleges.Find(id);
            return app;
        }

        /// <summary>
        /// SearchByKeyAndTime
        /// </summary>
        /// <param name="name">关键字(Title,Code)</param>
        /// <param name="province">起始时间</param>
        /// <param name="country">结束时间</param>
        /// <returns></returns>
        public IEnumerable<College> SearchByKeyAndTime(string name, string province, string country)
        {
            IEnumerable<College> apps = null;
            if (name == null && province == null && country == null)   //000
            {
                apps = FindAll();
            }
            else if (name != null && province == null && country == null)   //100
            {
                apps = from n in mydb.Colleges
                       where n.CollegeName.Contains(name) 
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            else if (name == null && province != null && country == null)   //010
            {
                apps = from n in mydb.Colleges
                       where n.Province.Contains(province)
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            else if (name == null && province == null && country != null)   //001
            {
                apps = from n in mydb.Colleges
                       where n.Country.Contains(country)
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            else if (name != null && province == null && country != null)   //101
            {
                apps = from n in mydb.Colleges
                       where n.CollegeName.Contains(name) || n.Country.Contains(country)
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            else if (name != null && province != null && country == null)   //110
            {
                apps = from n in mydb.Colleges
                       where n.CollegeName.Contains(name) || n.Province.Contains(province)
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            else if (name == null && province != null && country != null)   //011
            {
                apps = from n in mydb.Colleges
                       where n.Country.Contains(country) || n.Province.Contains(province)
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            else if (name != null && province != null && country != null)   //111
            {
                apps = from n in mydb.Colleges
                       where n.Country.Contains(country) || n.Province.Contains(province) || n.CollegeName.Contains(name)
                       orderby n.Country, n.Province, n.CollegeName ascending
                       select n;
            }
            return apps;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">College</param>
        /// <returns></returns>
        public bool Create(College model)
        {
            try
            {
                mydb.Colleges.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Create in table<Colleges>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">College</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            try
            {
                mydb.Colleges.Remove(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<Colleges>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }
    }
}